<?php
namespace app\http;
 
use think\worker\Server;
 
class Worker extends Server
{
 
	protected $socket = 'websocket://127.0.0.1:1234';
    protected $uidConnections = [];
    private $all = [];
    private $ids = [];
    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        if(array_key_exists($data,$this->ids)){
            $id = $this->ids[$data];
            $this->all[$id]->send("close");
        }
        
        if( substr($data,0,3) == 'uid'){
            $uid = substr($data,3);
            $this->ids[$uid] = $connection->id;
        }
    }
 
    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
        $connection->send('已建立连接');
        $this->all[$connection->id] = $connection;
    }
    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    { 
        // 去除断开连接的客户端
        unset($this->all[$connection->id]);
    }
}